<!DOCTYPE html>
<html>
<head>
<title>拼团商品管理</title>
#include("../../include/common.html")
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>拼团商品管理</h5>
            </div>
           	<form id="listForm" action="list" method="get">
	            <div class="ibox-content">
	                <!-- 列表 start -->
	                <div class="form-horizontal clear">
	                    <div class="form-group">
	                        <div class="col-sm-8">
	                        	<a href="#(base)/admin/group/list" class="btn btn-primary"><i class="fa fa-arrow-left"></i> 返回</a>
	                            <a class="btn btn-primary" href="add?groupId=#(groupId)"><i class="fa fa-plus"></i> 添加</a>
	                            <a href="javascript:;" class="btn btn-warning disabled" id="deleteButton"><i class="fa fa-trash"></i> 删除</a>
	                            <a href="javascript:;" class="btn btn-primary" id="refreshButton"><i class="fa fa-refresh"></i> 刷新</a>
	                            <a href="javascript:;" class="btn btn-primary" id="sortButton"><i class="fa fa-sort"></i> 排序</a>
	                        </div>
	                    </div>
	                    <div class="table-responsive">
	                        <table class="table table-bordered" id="listTable">
	                            <thead>
                                    <tr>
                                    	<th width="40" class="text-center"><input type="checkbox" id="selectAll" /></th>
                                        <th>ID</th>
                                        <th>商品名称</th>
                                        <th>商品图片</th>
                                        <th>拼团人数</th>
                                        <th>拼团价格</th>
                                        <th>商品原价</th>
                                        <th width="80">排序</th>
                                        <th>创建日期</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody id="listTr">
                                    #for(groupProduct : page.list)
                                    <tr>
                                    	<td class="text-center"><input type="checkbox" name="ids" value="#(groupProduct.id)" /></td>
                                        <td>#(groupProduct.id)</td>
                                        <td>#(groupProduct.product.name)</td>
                                       	<td><img src="#(imageUrl(groupProduct.product.image))" height="50"/> </td>
                                        <td>#(groupProduct.num)</td>
                                        <td>#(groupProduct.price)</td>
                                        <td>#(groupProduct.product.price)</td>
                                        <td><input type="text" class="form-control" name="sorts" value="#(groupProduct.sort)" data-oldsort="#(groupProduct.sort)" data-id="#(groupProduct.id)"/></td>
                                        <td>#date(groupProduct.createDate,"yyyy-MM-dd")</td>
                                        <td>
                                           	<a href="edit?id=#(groupProduct.id)">[编辑]</a>
                                        </td>
                                    </tr>
                                    #end
                                </tbody>
	                        </table>
	                    </div>
	                    <div class="text-center">
	                           #pagination(pageNumber = page.pageNumber,totalPages = page.totalPage,pattern = "")
							        #include("../../include/pagination.html")
					           #end
	                    </div>
	                </div>
	                <!-- 列表 end -->
	            </div>
            </form>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
$().ready(function() {

    var $sortButton = $("#sortButton");
    
    //排序
    $sortButton.click(function() {
        var $this = $(this);
        var $sortArry = [];
        $.each($("#listTr tr"),function(index,item){
            var $sorts = $(item).find("input[name='sorts']");
            var $sort = $sorts.val();
            var $oldsort = $sorts.data("oldsort");
            var $id = $sorts.data("id");
            if($sort!=$oldsort){
                var $sortReg = /^[1-9]\d*|0$/;
                if (!($sortReg.test($sort))) { 
                   $sort = "0";
                }
                var $json = {"id":$id,"sort":$sort};
                $sortArry.push($json);
            }
        });
        if($sortArry.length<1){
            swal({title:'', text:'数据未修改!',icon: 'warning'});
            return false;
        }
        $.ajax({
            url: "updateSort",
            type: "POST",
            data: {sortArray: JSON.stringify($sortArry)},
            dataType: "json",
            cache: false,
            success: function(response) {
                if (response.type == "success") {
                    location.reload();
                }else{
                    swal({title:'', text:response.msg,icon: 'error'});
                }
            }
        });
        return false;
    });

});
</script>